package easy;

import java.util.HashSet;

/**
 * @author admin
 * 804. 唯一摩尔斯密码词
 * 解法一：用一个String来保存26个字母，先获得每个单词中每个字母在temp中的下标，再去
 * morse数组里找到对应的String，将这些String拼接并放入HashSet，结果为hashset的大小
 *
 * 解法二：原理基本同解法一，区别是字母的下标不需要通过temp来求得，通过ASCII码的差值来获得即可
 * tip:两个char型相减为ASCII码之差，如'b' - 'a' = 1
 */
public class LeeCode804 {
    public int uniqueMorseRepresentations(String[] words) {

//        解法一：
//        String temp = "abcdefghijklmnopqrstuvwxyz";
//        String[] morse = new String[]{".-","-...","-.-.","-..",".","..-.","--.",
//                "....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",
//                ".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
//        String res = "";
//        HashSet<String> set = new HashSet<>();
//        for (String word : words) {
//            for (int i = 0; i < word.length(); i++) {
//                int indexOf = temp.indexOf(word.charAt(i));
//                res = res + morse[indexOf];
//            }
//            set.add(res);
//            res = "";
//        }
//
//        return set.size();

//        解法二:
        String[] morse = new String[]{".-","-...","-.-.","-..",".","..-.","--.",
                "....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",
                ".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        HashSet<String> set = new HashSet<>();
        for (String word : words) {
            StringBuilder builder = new StringBuilder();
            for (char c : word.toCharArray()) {
                builder.append(morse[c - 'a']);
            }
            set.add(builder.toString());
        }
        return set.size();
    }

    public static void main(String[] args) {
        System.out.println(new LeeCode804().uniqueMorseRepresentations(new String[]{"gin", "zen", "gig", "msg"}));
    }

}
